﻿using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wemail.DAL
{
    public class SqlServerAccess

    {    
      //读取app.config中数据库连接字符串
        string connectionString = ConfigurationManager.ConnectionStrings["demoDB"].ConnectionString;
        public bool IsValid(string username, string password)
        {   //读取app.config中数据库连接字符串
           
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                // 查询数据库获取对应用户记录
                string query = "SELECT COUNT(*) FROM [user] WHERE username = @username AND password = @password";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@username", username);
                    command.Parameters.AddWithValue("@password", password);

                    int count = (int)command.ExecuteScalar();

                    if (count > 0)
                    {
                        return true;  // 用户名和密码有效
                    }
                    else
                    {
                        return false;  // 用户名和密码无效
                    }
                }
            }
        }
    }
}
